<?php

namespace App\Admin\Controllers;

use App\Http\Repositories\Redis\CacheDataRedis;
use App\Http\Traits\GlobalConstant;
use App\Models\Project;
use App\Models\SurveyTemplate;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class SurveyTemplateController extends BaseController
{

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('入户调研模板')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('入户调研模板详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑入户调研模板')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        $action = url('admin/project/update');
        return $content
            ->header('新增入户调研模板')
            ->description('新增入户调研模板')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SurveyTemplate());
        $grid->model()->orderBy('id', 'desc');

        $grid->column('key_assign', '模板名称')->modal('模板详情', function ($model) {
            $comments = $model->where('id', $model->id)->first();
            $arr      = $comments['val_assign'];
            if (!empty($arr)) {
                foreach ($arr as &$item) {
                    $item['input_type'] = GlobalConstant::SURVEY_TYPE[$item['input_type']];
                }
                unset($item);
            }
            return new Table(['字段名称', '类型'], $arr);
        });
        $grid->column('created_at', '创建时间');
        $grid->column('status', '状态')->display(function ($v) {
            return $v == 1 ? '正常' : '禁用';
        })->label([1 => 'primary', 2 => 'danger']);
        $grid->actions(function ($actions) {
            $actions->disableView();

        });
        $grid->disableBatchActions();
        $grid->tools(function ($tools) {
//            $tools->append(new LatLngImport());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->disableFilter();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Project::findOrFail($id));

        $show->field('created_at', '下单时间');
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
                $tools->disableEdit();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new SurveyTemplate());
        $form->display('id', '模板ID');
        $form->text('key_assign', '名称')->help('如：人物基础信息');
        $form->table('val_assign', '', function ($table) {
            $table->text('title','字段名称')->help('如：户主信息');
            $table->select('input_type','字段类型')->options(GlobalConstant::SURVEY_TYPE);
        });
        $form->radioButton('status', '状态')->options([0 => '禁用', 1 => '正常'])->default(1);

        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        $form->saved(function () {
            CacheDataRedis::getRedisInstance()->surveyTemplateCache();
        });
        return $form;
    }


}
